var app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: "",
    hiddenName: false,
    // .js
    multiArray: [['明天', '后天'], ['上午7点-11点', '下午1点-6点']],
    multiIndex: [0, 0],
    content: "",
   
  },

  onLoad: function (options) {//上一个页面传递过来的数据都会在options里
    var goods = JSON.parse(options.goods);
    var num = goods.length;
    var money = 0;
    for (var i = 0; i < goods.length; i++) {
      money = goods[i].price * goods[i].num + money;
    }
    this.setData({
      goods: goods,
      margin_top: 20 + num * 40,
      money: money
    })
  },
  radiochange: function (res) {
    let that = this;
    if (res.detail.value === 'send') {
      var money = this.data.money;
      that.setData({
        hiddenName: true,
        money: money + 5,
      })
    } else {
      that.setData({
        hiddenName: false,
      })
    }
  },
  inlocation: function (res) {
    let that = this;
    that.setData({
      location: res.detail.value
    })
  },
  bindMultiPickerChange: function(e){
    this.setData({
      multiIndex: e.detail.value
    })
  },
  getInputValue: function(e){
    this.setData({
      content: e.detail.value
    })
  },
  pay: function (res) {
    if (app.globalData.id!=""){
    var goods=[]
    for(var i=0;i<this.data.goods.length;i++){
     var item= {
        id: this.data.goods[i].id,
        num: this.data.goods[i].num
      }
      goods.push(item);
      };
       var send1;
    if (this.data.hiddenName==true){
      send1=1;
    }else{
      send1=0;
    }
    var data={
      remarks:this.data.content,
      location: this.data.location,
      multiIndex: this.data.multiIndex,
      customer: app.globalData.id,
      money: this.data.money,
      send: send1,
      goods:goods
    };
      var url = "http://" + app.globalData.ipv4 +":8080/order/insertOrder";
    wx.request({
      url: url,
      method: 'POST',
      data: data,
      header: { 'content-type': 'application/json' },
      success: function(res){
       if(res.data===1){
         wx.navigateTo({
           url: '../../components/success/success',
         })
       }else{
         wx.showToast({
           title: '系统繁忙，暂不可下单',
           icon: 'none',
           duration: 1500
         });
       }
      }
    })
    }else{
      wx.showToast({
        title: '请先登录后再下单',
        icon: 'none',
        duration: 1500
      });
      setTimeout(function(){
        wx.navigateTo({
          url: '../../components/bindnumber/bindnumber',
        })
      },1500);
    }
  },
}
)